<template>
    <van-button
      class="follow-btn"
      type="info"
      color="#3296fa"
      round
      :loading="isFollowLoading"
      v-if="!isFollow"
      size="small"
      @click="articleIsFollowed"
      icon="plus"
      >关注</van-button
    >
    <van-button
      class="follow-btn"
      round
      :loading="isFollowLoading"
      v-else
      @click="articleIsFollowed"
      size="small"
      >已关注</van-button
    >
</template>

<script>
import { followArticle, deleteArticle } from '@/api/user.js'
export default {
  props: {
    isFollow: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [String, Number, Object],
      required: true
    }
  },
  model: {
    prop: 'isFollow',
    event: 'updateFollowed'
  },
  data () {
    return {
      isFollowLoading: false // 关注按钮loading
    }
  },

  created () {},

  methods: {
    // 判断是否关注时所采取的操作
    async articleIsFollowed () {
      this.isFollowLoading = true
      try {
        if (this.isFollow) {
        // 已关注 做：取消关注
          await deleteArticle(this.autId)
        } else {
        // 未关注 做：关注操作
          await followArticle(this.autId)
        }
        // this.isFollow = !this.isFollow
        this.$emit('updateFollowed', !this.isFollow)
        this.isFollowLoading = false
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
